<template>
	<!-- 使能页面 -->
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row class="ml10" v-show="showSearch">
				<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
					<el-form-item label="名称" prop="enableName">
						<el-input placeholder="请输入名称" style="max-width: 180px" v-model="state.queryForm.enableName" />
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="search" type="primary">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button @click="resetQuery" icon="Refresh">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button v-auth="'exsecs_sceneLinkage_add'" @click="formDialogRef.openDialog()" class="ml10" type="primary">
						{{ $t('common.addBtn') }}
					</el-button>
					<el-button
						class="filter-item"
						v-auth="'access_accessSupplier_import'"
						:disabled="multiple"
						type="primary"
						icon="delete"
						@click="handleDeleteAll"
						>删除
					</el-button>

					<!-- 导出 -->
					<!-- @exportExcel="exportExcel" -->
					<right-toolbar
						:export="'sys_client_del'"
						@queryTable="getDataList"
						class="ml10"
						style="float: right; margin-right: 20px"
						v-model:showSearch="showSearch"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				style="width: 100%"
				row-key="id"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@select-all="handleSelectAll"
				@select="handleSelect"
			>
				<!-- 复选框 -->
				<el-table-column align="center" type="selection" width="40" />
				<el-table-column :label="t('scene.index')" type="index" width="60" />
				<el-table-column label="名称" prop="enableName" show-overflow-tooltip />
				<el-table-column label="别名" prop="byName" show-overflow-tooltip />
				<el-table-column label="告警配置" width="180">
					<template #default="scope">
						<div v-for="(i, index) in scope.row.alarmEnableBindList" :key="index">
							<div v-for="(it, index1) in alarmConfigOptions" :key="index1">
								<div v-if="it.typeCode == i.typeCode">{{ it.typeName }}</div>
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="触发器模式" width="180">
					<template #default="scope">
						<div style="" v-if="scope.row.type == 0">间隔触发</div>
						<div style="" v-if="scope.row.type == 1">立即触发</div>
					</template>
				</el-table-column>
				<el-table-column label="触发次数" prop="num" width="160" />
				<!-- <el-table-column label="间隔时间" prop="intervalTime" width="200" /> -->
				<el-table-column label="开关" show-overflow-tooltip width="120">
					<template #default="scope">
						<div style="" v-if="scope.row.openOrClose == 0">关</div>
						<div style="" v-if="scope.row.openOrClose == 1">开</div>
					</template>
				</el-table-column>
				<el-table-column :label="$t('common.action')" width="300">
					<template #default="scope">
						<el-switch
							v-model="scope.row.openOrClose"
							inline-prompt
							active-value="1"
							inactive-value="0"
							active-text="开"
							inactive-text="关"
							@change="handleSwitch(scope.row)"
							style="margin-right: 15px"
						/>
						<el-button icon="Edit" @click="formDialogRef.openDialog(scope.row.enableId)" text type="primary" v-auth="'alarm_alarmEnable_edit'"
							>{{ $t('common.editBtn') }}
						</el-button>
						<!-- 删除 -->
						<el-button icon="delete" @click="handleDelete(scope.row.enableId)" text type="primary" v-auth="'alarm_alarmEnable_del'"> 删除 </el-button>
					</template>
				</el-table-column>
				<template #empty>
					<div class="zanwu_img">
						<img src="../../../assets/img/zanwu.png" alt="" />
						<div class="font">暂无数据</div>
					</div>
				</template>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>
		<!-- 编辑、新增  -->
		<form-dialog @refresh="getDataList()" ref="formDialogRef" />
		<!-- 详情 -->
		<!-- <Details-page @refresh="getDataList()" ref="formDialogDetailsRef" /> -->
		<!-- <form-dialog-log @refresh="getDataList()" ref="permessionRef" /> -->
	</div>
</template>

<script lang="ts" name="systemSysOauthClientDetails" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { delRuleObj, fetchList, putObj, queryAlarmTypeConf } from '/@/api/alarm/enable';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useDict } from '/@/hooks/dict';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { computed } from 'vue';
import { array } from 'vue-types';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// const DetailsPage = defineAsyncComponent(() => import('./details.vue'));
// const FormDialogLog = defineAsyncComponent(() => import('./formLog.vue'));
const { t } = useI18n();
// 定义变量内容
const formDialogRef = ref();
const switchValue = ref(true);
const permessionRef = ref();
const formDialogDetailsRef = ref(); // 详情
const queryRef = ref();
// 搜索变量
const showSearch = ref(true);
const aaaa = ref([]);
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		// actionType: '1',
		// alarmEnableBindList: JSON.stringify(aaaa.value),
	},
	pageList: fetchList,
});

const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// // 导出excel
// const exportExcel = () => {
// 	downBlobFile('/admin/client/export', Object.assign(state.queryForm, { ids: selectObjs }), 'client.xlsx');
// };

const resetQuery = () => {
	queryRef.value.resetFields();
	// state.queryForm = {};

	getDataList();
};
const alarmConfigOptions = ref([]);
const getAlarmConfigOptions = () => {
	queryAlarmTypeConf().then((res) => {
		alarmConfigOptions.value = res.data.records.map((item) => {
			item.id = item.id + '';
			return { ...item };
		});
	});
};

// 单选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);
const handleSelect = async (val) => {
	selectObjs._rawValue = val.map(({ enableId }) => enableId);
	multiple.value = !selectObjs._rawValue.length;
};
// 多选变量
const handleSelectAll = async (val) => {
	selectObjs._rawValue = val.map(({ enableId }) => enableId);
	multiple.value = !selectObjs._rawValue.length;
};
// 单 删除操作
const handleDelete = async (objs) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}
	try {
		await delRuleObj(objs.split(','));
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 多 删除操作
const handleDeleteAll = async () => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}
	try {
		await delRuleObj(selectObjs._rawValue);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 开关事件
const handleSwitch = async (val) => {
	var objDataList = [];
	if (val.byName !== 'enable') {
		val.alarmEnableBindList.map((item) => {
			var obj = {};
			obj.typeCode = item;
			objDataList.push(obj);
		});
	} else {
		val.alarmEnableBindList.map((item) => {
			if (item.flag == '0') {
				var obj = {};
				obj.typeCode = item.typeCode;
				obj.flag = item.flag;
				objDataList.push(obj);
			} else {
				var obj = {};
				obj.typeCode = item.typeCode;
				obj.flag = item.flag;
				objDataList.push(obj);
			}
		});
	}
	var obj = {
		enableId: val.enableId,
		enableName: val.enableName, // 名称
		byName: val.byName, // 别名
		type: val.type,
		num: val.num,
		alarmEnableBindList: objDataList,
		// intervalTime: val.intervalTime,
		openOrClose: val.openOrClose,
	};
	try {
		await useMessageBox().confirm('确认要执行此操作吗?');
	} catch {
		getDataList();
		return;
	}
	try {
		await putObj(obj);
		getDataList();
		useMessage().success('操作成功！');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
const router = useRouter();

const route = useRoute();
onMounted(() => {
	getAlarmConfigOptions();
});
</script>
<style lang="scss" scoped>
.zanwu_img {
	img {
		margin: auto;
	}
	.font {
		display: flex;
		justify-content: center;
		margin-top: 10px;
		color: #546c88;
	}
}
</style>
